﻿@using IOA.MES.DataAccess.Enum
@{
    ViewBag.Title = "仓库管理";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
    var workshops = ViewBag.Workshops as List<IOA.MES.DataAccess.VModel.VWorkshop>;
}
@model List<IOA.MES.DataAccess.VModel.VWorkshop>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">仓库类型：</label>
                    <div class="layui-input-inline">
                        <select id="selWarehouseType" lay-filter="selWarehouseType">
                            <option value="-1" selected>所有类型</option>
                            <option value="@((int)WarehouseType.Material)">物料仓</option>
                            <option value="@((int)WarehouseType.Workshop)">线边仓</option>
                            <option value="@((int)WarehouseType.Product)">成品仓</option>
                            <option value="@((int)WarehouseType.Other)">其他仓库</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">车间：</label>
                    <div class="layui-input-inline">
                        <select id="selWorkshops" lay-filter="selWorkshops">
                            <option value="-2">所有仓库</option>
                            <option value="0">非线边仓</option>
                            <option value="-1">所有线边仓</option>
                            @foreach (var item in workshops)
                            {
                                <option value="@item.PkId">@item.WorkshopName</option>
                            }
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">仓库状态：</label>
                    <div class="layui-input-inline">
                        <select id="selStatus" lay-filter="selStatus">
                            <option value="-1">所有状态</option>
                            <option value="@((int)CommonStatus.Normal)" selected>正常</option>
                            <option value="@((int)CommonStatus.Disabled)">禁用</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button id="btnSearch" class="layui-btn layuiadmin-btn-list" type="button">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                    <button class="layui-btn layui-btn-normal layuiadmin-btn-list" type="button" onclick="editWarehouse(0);">
                        <i class="layui-icon layui-icon-addition layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>
    </div>

    <div class="layui-card">
        <div class="layui-card-body">
            <table id="table" lay-filter="table"></table>
        </div>
    </div>
</div>

@section Scripts{
    <script>
        initLayui();
        function initLayui() {
            layui.use(['table', 'layer', 'laydate', 'form'], function () {
                var layer = layui.layer,
                    table = layui.table,
                    laydate = layui.laydate,
                    form = layui.form;

                var options = {
                    elem: '#table'
                    , url: '/Warehouse/QueryWarehouses'
                    , method: "post"
                    , where: { warehouseType: null, workshopId: null, status: 1 }
                    , response: {
                        statusName: 'ErrCode'
                        , statusCode: 200
                        , msgName: 'ErrCode'
                        , countName: 'TotalCount'
                        , dataName: 'Data'
                    }
                    , cols: [[
                        { title: '序号', type: "numbers", width: 60 }
                        , { field: 'CNWarehouseType', title: '仓库类型', minWidth: 100 }
                        , { field: 'WarehouseCode', title: '仓库代码', minWidth: 100 }
                        , { field: 'WarehouseName', title: '仓库名称', minWidth: 150 }
                        , { field: 'WorkshopName', title: '所属车间', minWidth: 100 }
                        , { field: 'CNStatus', title: '仓库状态', minWidth: 100 }
                        , { field: 'CreateTimeStr', title: '创建时间', minWidth: 150 }
                        , {
                            title: '操作', width: 280, fixed: "right", templet: function (d) {
                                var str = '<button class="layui-btn layui-btn-sm layui-btn-normal" onclick="editWarehouse(' + d.PkId + ')"><i class="layui-icon">&#xe642;</i>编辑</button>\
                                               <button class="layui-btn layui-btn-sm" onclick="editWarehouseUser(\'' + d.WarehouseCode + '\')"><i class="layui-icon">&#xe642;</i>成员管理</button>\
                                               <button class="layui-btn layui-btn-sm layui-btn-danger" onclick="modifyStatus(' + d.PkId + ',' + (1 - d.Status) + ')"><i class="layui-icon">&#xe616;</i>' + (d.Status == 0 ? "启用" : "禁用") + '</button>';
                                return str;
                            }
                        }
                    ]]
                };

                $("#btnSearch").click(function () {
                    queryTable();
                });
                queryTable();
                function queryTable() {
                    var warehouseType = parseInt($("#selWarehouseType").val().trim()),
                        status = parseInt($("#selStatus").val().trim()),
                        workshopId = parseInt($("#selWorkshops").val().trim());

                    options.where.warehouseType = warehouseType == -1 ? null : warehouseType;
                    options.where.status = status == -1 ? null : status;
                    options.where.workshopId = workshopId;

                    if (table.index <= 0) {
                        table.render(options);
                    } else {
                        table.reload("table", options);
                    }
                }
            });
        }

        function editWarehouse(warehouseId) {
            layer.open({
                type: 2,
                shade: [0.1, '#000'],
                area: ['550px', '400px'],
                content: "/Warehouse/WarehouseEdit?warehouseId=" + warehouseId,
                end: function () {
                    $("#btnSearch").click();
                }
            });
        }

        function editWarehouseUser(warehouseCode) {
            layer.open({
                type: 2,
                shade: [0.1, '#000'],
                area: ['550px', '500px'],
                content: "/Warehouse/WarehouseUserEdit?warehouseCode=" + warehouseCode
            });
        }

        function modifyStatus(warehouseId, status) {
            var loadId = layer.load();
            $.ajax({
                url: "/Warehouse/ModifyWarehouseStatus",
                data: {
                    warehouseId: warehouseId,
                    status: status
                },
                complete: function () {
                    layer.close(loadId);
                },
                success: function (res) {
                    if (res.ErrCode != 200) {
                        layer.msg(res.ErrMsg, { icon: 5 });
                        return;
                    }
                    layer.msg("修改成功！", { icon: 6, time: 1500 });
                    $("#btnSearch").click();
                }
            });
        }
    </script>
}
